package zhihui.mobile.ui.shop.cart;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zgm.utils.ui.UiTextHeader;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.ShoppingCartPresenter;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.shop.attention.ShopAttentionActivity;
import zhihui.mobile.ui.shop.order.ShopOrderConfirmActivity;

/**
 * Created by 张公铭 on 2017/8/8 0008.
 */
public class ShoppingCartActivity extends BaseActivity implements IShoppingCartContract.IShoppingCartView
{
    private ShoppingCartPresenter cartPresenter = new ShoppingCartPresenter();
    private UiTextHeader mheader;
    private ListView lv_content;
    private CheckBox cb_all;
    private TextView tv_money;
    private TextView tv_num;
    private RelativeLayout rl_buy;
    private RelativeLayout rl_footer_buy;
    private RelativeLayout rl_footer_edit;
    private CheckBox cb_edit_all;
    private Button btn_delete;
    private Button btn_share;
    private Button btn_attention;
    private Button btn_go_sales;
    private Button btn_go_attention;
    private LinearLayout ll_noraml;

    private ShopCartAdapter cartAdapter;
    private int cartNum = 0;

    @Override
    public void initView()
    {
        mheader = (UiTextHeader) findViewById(R.id.shopping_cart_header);
        mheader.setBackgroundColor("#fafafa");
        mheader.setTitleColor("#333333");
        mheader.setTitle("购物车");
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                cartPresenter.updateCart();
            }
        });

        mheader.setRightText("编辑");
        mheader.setRightTextColor("#666666");
        mheader.setRightTextListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (((TextView) v).getText().equals("编辑"))
                {
                    mheader.setRightText("完成");
                    cartAdapter.setAll(false);
                    cartAdapter.notifyDataSetChanged();
                    rl_footer_buy.setVisibility(View.INVISIBLE);
                    rl_footer_edit.setVisibility(View.VISIBLE);

                } else
                {
                    mheader.setRightText("编辑");
                    cartAdapter.setAll(true);
                    tv_money.setText("0.00");
                    cartPresenter.initData();
                    cb_all.setChecked(true);
                    rl_footer_buy.setVisibility(View.VISIBLE);
                    rl_footer_edit.setVisibility(View.INVISIBLE);
                }
            }
        });

        lv_content = (ListView) findViewById(R.id.lv_shopping_cart_content);
        cartAdapter = new ShopCartAdapter(this);
        cartAdapter.setListener(new ShopCartAdapter.IShopNumberChangeListener()
        {

            @Override
            public void addPrice(int price)
            {
                double money = Double.parseDouble(tv_money.getText().toString());
                BigDecimal bg = new BigDecimal(money * 100);
                double tempPrice = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                int addPrice = (int) (tempPrice + price);
                tv_money.setText(new DecimalFormat("#0.00").format((double) addPrice / 100));
            }

            @Override
            public void subtractPrice(int price)
            {
                double money = Double.parseDouble(tv_money.getText().toString());
                BigDecimal bg = new BigDecimal(money * 100);
                double tempPrice = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                int subtrctPrice = (int) (tempPrice - price);
                tv_money.setText(new DecimalFormat("#0.00").format((double) subtrctPrice / 100));
            }

            @Override
            public void updateCountNum(int num)
            {
                cartNum += num;
                mheader.setTitle("购物车(" + cartNum + ")");
            }

            @Override
            public void updateBuyNum(int num)
            {
                int buyNum = Integer.parseInt(tv_num.getText().toString());
                buyNum += num;
                tv_num.setText(buyNum + "");
            }
        });
        lv_content.setAdapter(cartAdapter);

        cb_all = (CheckBox) findViewById(R.id.cb_shopping_cart_all_select);
        cb_all.setChecked(true);
        cb_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cartAdapter.setAll(true);
                    cartAdapter.notifyDataSetChanged();
                } else
                {
                    cartAdapter.setAll(false);
                    cartAdapter.notifyDataSetChanged();
                }
            }
        });

        tv_money = (TextView) findViewById(R.id.tv_shopping_cart_price);
        tv_money.setText("0.00");
        tv_num = (TextView) findViewById(R.id.tv_shopping_cart_buy_num);
        rl_buy = (RelativeLayout) findViewById(R.id.rl_shopping_cart_buy);
        rl_buy.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //下单
                cartPresenter.comfirmOrder();
            }
        });

        rl_footer_buy = (RelativeLayout) findViewById(R.id.rl_shopping_cart_footer);
        rl_footer_buy.setVisibility(View.INVISIBLE);

        rl_footer_edit = (RelativeLayout) findViewById(R.id.rl_shopping_cart_footer_edit);
        rl_footer_edit.setVisibility(View.INVISIBLE);

        cb_edit_all = (CheckBox) findViewById(R.id.cb_shopping_cart_all_select_edit);
        cb_edit_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if (isChecked)
                {
                    cartAdapter.setAll(true);
                    cartAdapter.notifyDataSetChanged();
                } else
                {
                    cartAdapter.setAll(false);
                    cartAdapter.notifyDataSetChanged();
                }
            }
        });

        btn_delete = (Button) findViewById(R.id.btn_shopping_cart_delete);
        btn_delete.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //删除
                cartPresenter.delete();
            }
        });

        btn_attention = (Button) findViewById(R.id.btn_shopping_cart_attention);
        btn_attention.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //关注
                cartPresenter.attention();
            }
        });

        btn_share = (Button) findViewById(R.id.btn_shopping_cart_share);
        btn_share.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //分享
            }
        });

        btn_go_attention = (Button) findViewById(R.id.btn_shopping_cart_go_attention);
        btn_go_attention.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //去关注
                cartPresenter.goAttention();
            }
        });

        btn_go_sales = (Button) findViewById(R.id.btn_shopping_cart_go_sales);
        btn_go_sales.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //去逛逛
                cartPresenter.goSalesActivity(ShoppingCartActivity.this, ShoppingCartActivity.this, 1);
            }
        });

        ll_noraml = (LinearLayout) findViewById(R.id.ll_shopping_cart_normal);
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shopping_cart_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{cartPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        cartPresenter.init(this, this);
    }

    @Override
    public void updateCartNum(int num)
    {
        if (num > 0)
        {
            lv_content.setVisibility(View.VISIBLE);
            ll_noraml.setVisibility(View.GONE);
            mheader.setTitle("购物车(" + num + ")");
            cartNum = num;
            tv_num.setText(0 + "");
        } else
        {
            mheader.setTitle("购物车");
            ll_noraml.setVisibility(View.VISIBLE);
            lv_content.setVisibility(View.GONE);
            rl_footer_edit.setVisibility(View.GONE);
            rl_footer_buy.setVisibility(View.GONE);
        }
    }

    @Override
    public void goComfirmOrderActivity()
    {
        Intent intent = new Intent(this, ShopOrderConfirmActivity.class);
        startActivity(intent);
    }

    @Override
    public void initList(List<SkuInfo> list)
    {
        cartAdapter.setList(list);
        cartAdapter.notifyDataSetChanged();
        if (list.size() > 0)
        {
            rl_footer_buy.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void goShopAttention()
    {
        Intent intent = new Intent(this, ShopAttentionActivity.class);
        startActivity(intent);
    }
}
